﻿// Copyright (c) 2010 Dan Popick

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace Open.Web.Stijl.Utility
{
    public struct ColorHistogram
    {
        public ulong Value;

        public ColorHistogram(ulong val)
        {
            Value = val;
        }

        public int BitCount { get { return SparseBitcount(Value); } }

        public ColorHistogram(IEnumerable<Color> colors)
            :this(0)
        {
            ulong val = 0;

            foreach (Color c in colors)
            {
                float hue = c.GetHue();
                float bright = c.GetBrightness();
                float sat = c.GetSaturation();

                if (sat == 0)
                {
                    ulong boff = (ulong)(bright * 16.0);
                    val |= (boff << 60);
                }
                else
                {
                    int boff = (int)(bright * 2.0);
                    int hoff = (int)(hue * 60.0 / 360.0);
                    val |= ((ulong)1 << (hoff + boff));
                }
            }

            Value = val;
        }

        public static ColorHistogram operator | (ColorHistogram lhs, ColorHistogram rhs)
        {
            return new ColorHistogram(lhs.Value | rhs.Value);
        }

        int SparseBitcount(uint val)
        {
            int count = 0;
            while (val != 0)
            {
                count++;
                val &= (val - 1);
            }
            return count;
        }

        int SparseBitcount(ulong n)
        {
            return SparseBitcount((uint)(n& 0xFfffFfff)) + SparseBitcount((uint)(n >> 32));
        }

        public int ComputeOverlap(ColorHistogram hist)
        {
            return SparseBitcount(Value ^ hist.Value);
        }

        static public bool operator ==(ColorHistogram lhs, ColorHistogram rhs)
        {
            return lhs.Value == rhs.Value;
        }

        static public bool operator !=(ColorHistogram lhs, ColorHistogram rhs)
        {
            return lhs.Value != rhs.Value;
        }

        public override bool Equals(object obj)
        {
            return (obj is ColorHistogram) ? ((ColorHistogram)obj).Value == Value : false;
        }

        public override int GetHashCode()
        {
            return (int)Value;
        }
    }
}
